
<?php
if (!defined("ACCESS"))
    die("Access denied");

$borrow_id = $_REQUEST["borrow_id"];
//$card_id = $_REQUEST["card_id"];

$db = new myDBS();

$sql = "SELECT budget_collect FROM budget_collect";
$r = $db->fetch_object($db->runSQL($sql));

$budget_collect = $r->budget_collect;

if ($borrow_id) {
    $sql = "SELECT * FROM tbl_promise_borrow WHERE borrow_id='$borrow_id'";
    $result1 = $db->runSQL($sql);
    $row1 = $db->fetch_object($result1);

    //$card_id = $row1->card_id;
}
//$db->selectDB("hr_udoncity");
//
//$sql = "select * from person where id = '$card_id'";
//
//$result = $db->runSQL($sql);
//$row = $db->fetch_object($result);
?>

<script type="text/javascript">
    $(document).ready(function() {
        $("#promise_date").datepicker({
            dateFormat: "yy-mm-dd"
//            ,showButtonPanel: true
//            ,defaultDate: new Date()
        });

        $("#mati_date").datepicker({
            dateFormat: "yy-mm-dd"
        });

        $("#maturity_date").datepicker({
            dateFormat: "yy-mm-dd"
        });

        $("#extend_date").datepicker({
            dateFormat: "yy-mm-dd"
        });

        $("#card_id").select2({
            placeholder: "เลือกผู้ขอยืม..."
            , allowClear: true
                    //, maximumSelectionSize: 1
                    //, tags:["red", "green", "blue"]
        });

//        $("#promise_id").select2({
//            placeholder: "เลือกเลขที่สัญญา..."
//                    //,allowClear: true
//                    //, maximumSelectionSize: 2
//                    //, tags:["red", "green", "blue"]
//        });

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                event.preventDefault();
                //alert("success");

                $.ajax({
                    type: "POST",
                    url: "promise_borrow_process.php",
                    data: $('#frm_main').serialize(),
                    cach: false,
                    error: function() {
                        alert("ERROR !!!!");
                    },
                    success: function(data) {

                        //$("#dispStatus").html(data);

                        window.location.href = "index.php?pages=promise_borrow_view";

                    }
                });
            }
        });



        $('#borrow_amount').number(true, 2);
        $('#borrow_amount').keyup(function() {

            var borrow_amount = $('#borrow_amount').val();
            var budget_remain = $('#budget_remain').val();

            var budget_remain_this = budget_remain - borrow_amount;
            $("#budget_remain_this").val(budget_remain_this);
        });

        $.fn.myCalc = function() {
//            var advances = $('#advances').val();
            var allowances = $('#allowances').val();
            var rest_rate = $('#rest_rate').val();
            var vehicle_rate = $('#vehicle_rate').val();
            var other = $('#other').val();
            var total = 0.00;

//            advances = parseFloat(advances);
            allowances = parseFloat(allowances);
            rest_rate = parseFloat(rest_rate);
            vehicle_rate = parseFloat(vehicle_rate);
            other = parseFloat(other);

//            total = advances + allowances + rest_rate + vehicle_rate + other;

            //alert(total);
            $("#total").val(total);
        }

        $('#advances').number(true, 2);
        $('#allowances').number(true, 2);
        $('#rest_rate').number(true, 2);
        $('#vehicle_rate').number(true, 2);
        $('#other').number(true, 2);
        $('#total').number(true, 2);

//        $("#advances").keyup(function() {
//            $.fn.myCalc();
//        });
//
//        $('#allowances').keyup(function() {
//            $.fn.myCalc();
//        });
//
//        $('#rest_rate').keyup(function() {
//            $.fn.myCalc();
//        });
//
//        $('#vehicle_rate').keyup(function() {
//            $.fn.myCalc();
//        });
//        $('#other').keyup(function() {
//            $.fn.myCalc();
//        });





//        $("#save_data").click(function() {
//            event.preventDefault();
//            alert("save_data");
//        });

    });


</script>

<div id="dispStatus"></div>

<form class="form-horizontal" id="frm_main" method="post" novalidate=""> 
    <!--<form class="form-horizontal" id="frm_main" name="frm_main"  method="post">-->
    <input type="hidden" id="borrow_id" name="borrow_id" value="<?php echo $borrow_id; ?>">
    <input type="hidden" id="task" name="task" value="<?php echo ($borrow_id) ? "edit" : "new"; ?>">
    <div class="row-fluid">
        <div class="span12">
            <div class="panel panel-info">
                <div class="panel-heading">สัญญายืมเงินสะสม</div>
                <div class="panel-body">
                    <div class="control-group">
                        <label class="control-label" for="fullname">ชื่อ นามสกุล</label>
                        <div class="controls">
                            <select  id="card_id" name="card_id" style="width: 250px;" required="">
                                <option value="" ></option>
                                <?php
                                $sql = "select * from person ";
                                //$db = new myDBS();
                                $db->selectDB("hr_udoncity");
                                $result = $db->runSQL($sql);
                                while ($row2 = mysql_fetch_object($result)) {
                                    ?>
                                    <option value="<?php echo $row2->id; ?>" <?php echo ($row1->card_id == $row2->id) ? " selected" : ""; ?>><?php echo $row2->fname . "  " . $row2->lname; ?></option>
                                    <?php
                                }
                                ?>
                            </select>
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="ตำแหน่ง">ตำแหน่ง</label>
                        <div class="controls">
                            <input type="text" id="" name="" value="" disabled="">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="สังกัด">สังกัด</label>
                        <div class="controls">
                            <input type="text" id="" name="" value="" disabled="">
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="มีความประสงค์ขอยืมเงินเพื่อ">มีความประสงค์ขอยืมเงินเพื่อ</label>
                        <div class="controls">
                            <textarea rows="5" class="span5" id="objective" name="objective"><?php echo $row1->objective; ?></textarea>
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label" for="ตามมติประชุมครั้งที่">ตามมติประชุมครั้งที่</label>
                        <div class="controls">
                            <input type="text" id="mati" name="mati" value="<?php echo $row1->mati; ?>" >
                        </div>
                    </div>

                    <div class="control-group">
                        <label class="control-label" for="วันที่">วันที่</label>
                        <div class="controls">
                            <input type="text" id="mati_date" name="mati_date" value="<?php echo $row1->mati_date; ?>">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row-fluid">
        <div class="panel panel-info">
            <div class="panel-heading">...</div>
            <div class="panel-body"> 
                <div class="span6">
                    <div class="control-group">
                        <label class="control-label" for="จำนวนเงินสะสมคงเหลือ">จำนวนเงินสะสมคงเหลือ</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="budget_remain" name="budget_remain" value="<?php echo ($id) ? $row1->budget_remain : $budget_collect; ?>" readonly="">
                        </div>
                    </div>


                    <div class="control-group">
                        <label class="control-label" for="จำนวนเงินที่ยืม">จำนวนเงินที่ยืม</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="borrow_amount" name="borrow_amount" value="<?php echo ($row1->borrow_amount) ? $row1->borrow_amount : ""; ?>" placeholder="0.00">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="หักเงินยืมครั้งนี้แล้วคงเหลือ">หักเงินยืมครั้งนี้แล้วคงเหลือ</label>
                        <div class="controls">
                            <input class="text-right" type="text" id="budget_remain_this" name="budget_remain_this" value="<?php echo ($id) ? $row1->budget_remain_this : ""; ?>" readonly="" placeholder="0.00">
                        </div>
                    </div>
                </div>
                <div class="span6">
                    <div class="control-group">
                        <label class="control-label" for="วันที่ทำสัญญา">วันที่ทำสัญญา</label>
                        <div class="controls">
                            <input type="text" id="promise_date" name="promise_date" value="<?php echo ($borrow_id) ? $row1->promise_date : date('Y-m-d'); ?>">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="วันที่ครบกำหนดชำระ">วันที่ครบกำหนดชำระ</label>
                        <div class="controls">
                            <input type="text" id="maturity_date" name="maturity_date" value="<?php echo ($borrow_id) ? $row1->maturity_date : date('Y-m-d'); ?>">
                        </div>
                    </div>
                    <div class="control-group">
                        <label class="control-label" for="วันที่ขยายเวลาการชำระคืน">วันที่ขยายเวลาการชำระคืน</label>
                        <div class="controls">
                            <input type="text" id="extend_date" name="extend_date" value="<?php echo ($borrow_id) ? $row1->extend_date : date('Y-m-d'); ?>" disabled="">
                        </div>
                    </div>
                </div>

                <div class="control-group">
                    <div class="controls">
                        <button class="btn btn-info" type="submit" >บันทึกข้อมูล</button>&nbsp;&nbsp;&nbsp;<a class="btn" href="index.php?pages=promise_borrow_view">ยกเลิก</a>
                    </div>  
                </div>
            </div>
        </div>

    </div>

</form>


<?php ?>
